package net.xuele.learn.executorPool;

import lombok.Builder;
import lombok.Data;
import org.jetbrains.annotations.NotNull;

import javax.security.auth.login.Configuration;
import java.util.*;
import java.util.concurrent.*;

/**
 * @Author: yubo
 * @date: 17:41 2020/7/3
 * @Description: 类描述
 */
@Data
@Builder
public class Person implements Comparable<Person> {

    private int id;

    private String data;


    public List<String> filterDisablePayment(String userId, List<String> allPaymentTypeList) throws InterruptedException {
        ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
        CountDownLatch l = new CountDownLatch(allPaymentTypeList.size());
        //: TODO 完成此处的代码
        allPaymentTypeList.parallelStream().forEach(
                e -> {
                    CompletableFuture.supplyAsync(() -> {
                        return isEnabled(userId, e);
                    }).whenComplete((v, t) -> {
                        if (t != null && v != null && v.isEnable) {
                            queue.add(e);
                        }
                        l.countDown();
                    });
                }
        );
        l.await();
        return Arrays.asList((String[]) queue.toArray());
    }

    @Override
    public int compareTo(@NotNull Person o) {
        return this.data.compareTo(o.getData());
    }


    public class ConsultResult {
        /**
         * 咨询结果是否可用
         */
        private boolean isEnable;
        /**
         * 错误码
         */
        private String errorCode;
    }


    ConsultResult isEnabled(String userId, String paymentType) {
        return new ConsultResult();
    }

    public static void main(String[] args) {
        List<Person> list = new LinkedList<>();
        Person p1 = Person.builder().id(1).data("a").build();
        list.add(p1);
        Person p2 = Person.builder().id(2).data("b").build();
        list.add(p2);
        Person p3 = Person.builder().id(3).data("c").build();
        list.add(p3);
        Person p4 = Person.builder().id(4).data("d").build();
        list.add(p4);
        Person p5 = Person.builder().id(5).data("a1").build();
        list.add(p5);
        Person p6 = Person.builder().id(6).data("b1").build();
        list.add(p6);
        Person p7 = Person.builder().id(7).data("c1").build();
        list.add(p7);
        Person p8 = Person.builder().id(8).data("d1").build();
        list.add(p8);
        Set<String> set = new HashSet<>();
        list.forEach(e -> {
            System.out.println("----" + e.getData());
            set.add(e.getData());
        });
        set.forEach(e -> {
            System.out.println(e);
        });
    }
}
